package com.mvetc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mvetc.dao.UserDao;
import com.mvetc.entity.PageInfo;
import com.mvetc.entity.User;
import com.mvetc.util.DBUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public int insert(User user) {
		
		return DBUtil.doUpdate("insert into user_info(user_id,user_name,user_password,user_picture,member_status) values(?,?,?,?,?)",user.getUserId(),user.getUserName(),user.getUserPassword(),user.getUserPicture(),user.getMemberStatus());
		
	}



	@Override
	public User findByUserNameAndPassword(String userName, String userPassword) {
		User user = null;
		ResultSet doQuery = DBUtil.doQuery("select user_id,user_name,user_password,user_picture,member_status from user_info where user_name = ? and user_password = ?"
				 ,userName,userPassword);
		try {
			while (doQuery.next()) {
				String userId = doQuery.getString("user_id");
				String userPicture = doQuery.getString("user_picture");
				String memberStatus = doQuery.getString("member_status");
				user = new User(userId, userName, userPassword, userPicture, memberStatus);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}



	@Override
	public User findByUserName(String userName) {
		User user = null;
		ResultSet doQuery = DBUtil.doQuery("select user_id,user_name,user_password,user_picture,member_status from user_info where user_name = ? "
				 ,userName);
		try {
			while (doQuery.next()) {
				String userId = doQuery.getString("user_id");
				String password = doQuery.getString("user_password");
				String userPicture = doQuery.getString("user_picture");
				String memberStatus = doQuery.getString("member_status");
				user = new User(userId, userName, password, userPicture, memberStatus);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public PageInfo<User> findBy(Map<String, String> params) {
		int pageNum = Integer.parseInt(params.get("pageNum"));
		int pageSize = Integer.parseInt(params.get("pageSize"));
		List<User> list = new ArrayList<>();
		ResultSet doQuery = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select user_id")
		.append(",user_name")
		.append(",user_password")
		.append(",user_picture")
		.append(",member_status")
		.append(" from user_info");
		if (!"".equals(params.get("userNameKey"))) {
			sb.append(" where user_name like ? limit ?,?");
			doQuery = DBUtil.doQuery(sb.toString(), "%"+params.get("userNameKey")+"%",(pageNum-1)*pageSize,pageSize);
		}else {
			sb.append(" limit ?,?");
			doQuery = DBUtil.doQuery(sb.toString(),(pageNum-1)*pageSize,pageSize);
		}
		try {
			while (doQuery.next()) {
				String userId = doQuery.getString("user_id");
				String userName = doQuery.getString("user_name");
				String userPassword = doQuery.getString("user_password");
				String userPicture = doQuery.getString("user_picture");
				String memberStatus = doQuery.getString("member_status");
				User user = new User(userId, userName, userPassword, userPicture, memberStatus, null, null);
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Integer total = 0;
		// 获取符合当前查询条件的总记录数
		doQuery = null;
		if (!"".equals(params.get("userNameKey"))) {
			doQuery = DBUtil.doQuery("select count(*) from user_info where user_name like ?","%"+params.get("userNameKey")+"%");
		}else {
			doQuery = DBUtil.doQuery("select count(*) from user_info");
		}
		
		try {
			while (doQuery.next()) {
				total = doQuery.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		PageInfo<User> pageInfo = new PageInfo<>(list, pageNum, pageSize, total);
		return pageInfo;
	}

	@Override
	public int deleteById(String userId) {
		int doUpdate = DBUtil.doUpdate("delete from user_info where user_id = ?", userId);
		return doUpdate;
	}

	@Override
	public int updateUser(User user) {
		int doUpdate = DBUtil.doUpdate("update user_info set member_status = ? where user_id = ?", user.getMemberStatus(),user.getUserId());
		return doUpdate;
	}

	
}
